﻿using System;
using System.Drawing;
using System.Windows.Forms;
using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using Sunny.UI;

namespace DevelopTool.Forms.CommonForms
{
    /// <summary>
    /// 倒计时消息提示窗体
    /// </summary>
    public partial class CountDownAskMessageForm : BaseUIForm
    {

        /// <summary>
        /// 窗体标题
        /// </summary>
        private string title;

        /// <summary>
        /// 提示消息
        /// </summary>
        private string msg;

        /// <summary>
        /// 倒计时
        /// </summary>
        private int countDown;

        private System.Windows.Forms.Timer timer;

        /// <summary>
        /// 倒计时消息提示窗体
        /// </summary>
        public CountDownAskMessageForm(string title,string msg,int countDown = 5,UIStyle uIStyle = UIStyle.Orange)
        {
            InitializeComponent();

            this.title = title;
            this.msg = msg;
            this.countDown = countDown;

            if(uIStyle != UIStyle.Inherited)
                this.Style = uIStyle;

            this.btnCancel.FillColor = Setting.Theme.BackColor;
            this.btnCancel.FillHoverColor = Color.Gray;
        }

        private void CountDownAskMessageForm_Load(object sender,EventArgs e)
        {
            this.Text = this.title;
            this.lbMsg.Text = this.msg;

            if(this.countDown > 0)
            {
                this.btnOK.Enabled = false;
                this.btnOK.Text = $"确定({this.countDown})";

                this.timer = new System.Windows.Forms.Timer
                {
                    Interval = 1000,
                    Enabled = true//启动计时器
                };
                timer.Tick += delegate (object ss,EventArgs ee)
                {
                    this.countDown--;
                    if(this.countDown <= 0)
                    {
                        this.timer.Stop();
                        this.btnOK.Enabled = true;
                        this.btnOK.Text = $"确定";
                    }
                    else
                    {
                        this.btnOK.Text = $"确定({this.countDown})";
                    }
                };
            }


            if(this.Style == UIStyle.Inherited)
            {
                ThemHelper.SetSystemThem(this);
            }
        }


        private void btnOK_Click(object sender,EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        private void btnCancel_Click(object sender,EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }


        private void CountDownAskMessageForm_FormClosed(object sender,FormClosedEventArgs e)
        {
            timer?.Dispose();
        }


    }
}